package com.btjava.maven.pomver.handler;

import com.btjava.maven.pomver.data.CommonData;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class DataHandler {

    public static void getRepositoryUrl(){
        //默认先读取jar配置
        getUrlByJarRepositories();
        if(Objects.isNull(CommonData.urlList) || CommonData.urlList.isEmpty()){
            getUrlByWorkSpace();
        }
    }

    private static void getUrlByJarRepositories(){
        //用于存放maven仓库地址
        List<String> urlList = JarRepositorieshandler.getMavenRepositoryUrl();
        if(Objects.nonNull(CommonData.urlList) && !CommonData.urlList.isEmpty()){
            CommonData.urlList = urlList;
        }
    }

    private static void getUrlByWorkSpace(){
        //用于存放maven仓库地址
        List<String> urlList = new ArrayList<>();
        //获取默认的maven仓库地址
        String repositoryUrl = WorkSpaceXmlHandler.getMavenRepositoryUrl( CommonData.workSpacePath);
        //如果不存在默认仓库地址，则从maven配置文件中获取
        CommonData.mavenSettingPath = WorkSpaceXmlHandler.getMavenSetting( CommonData.workSpacePath);
        //如果当前maven未配置第三方地址，则需要返回空，需要手动填写
        urlList = SettingXmlHandler.getMavenRepositoryUrl(CommonData.mavenSettingPath);
        if(StringUtils.isNotBlank(repositoryUrl) && !urlList.contains(repositoryUrl)){
            urlList.add(repositoryUrl);
        }
        CommonData.urlList = urlList;
    }
}
